package com.lyf.task.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lyf.task.dao.RfidMapper;
import com.lyf.task.domain.Rfid;
import com.lyf.task.result.rfidResult;
import com.lyf.task.service.RFIDUtilService;
import com.lyf.task.util.PbUtil;
import com.lyf.task.util.RFIDUtil;
import com.lyf.task.util.ToByteArr;
import com.lyf.task.util.ToHexing;


@Service
public class RFIDUtilServiceImpl implements RFIDUtilService {
	
	@Autowired
	private RfidMapper rfidMapper;

	@Override
	public  rfidResult read() {
		// TODO Auto-generated method stub
		System.out.println("read start");
		
		RFIDUtil mt=new RFIDUtil();
		
		ToHexing tx = new ToHexing();
      
		//RFID����
	   // mt.testinitreader();
	  //  byte[] data =mt.testread();
	   mt.Jreader.CloseReader();
	    
	 //��ݿ���Բ���
	    String rfidNum= rfidMapper.getRfidByPid(1000).get(0).getRfidnum();
	    byte[] data = ToByteArr.toByteArrary(rfidNum);
	    
	    PbUtil.readPrintf(data);
	    System.out.println("read over");
	    
	    String result="";
		
		for(int i=0;i<12;i++)
		{
			result+=Integer.toHexString(data[i]&0xff);
		}
	    System.out.println(result);
	    rfidResult rs = new rfidResult();
	    rs.setPid(Integer.parseInt(result.substring(0,4)));
	    
	    rs.setDate("20"+tx.toString1(result.substring(4,6))+"-"+tx.toString1(result.substring(6,7))+
	    		"-"+tx.toString1(result.substring(7, 9)));
	    
	    rs.setTradeTime(Integer.parseInt(tx.toString1(result.charAt(10)+"")));
	    
	    rs.setDegree(Integer.parseInt(tx.toString1(result.charAt(11)+"")));
	    
		return rs;
		
		
		
	}

	@Override
	public void writeNewRfid(rfidResult rs) {
		ToHexing tx = new ToHexing();
		// TODO Auto-generated method stub
		System.out.println("write start");
		RFIDUtil mt=new RFIDUtil();
        //���Գ�ʼ��
		
		
		//��ݴ���
	    String pid = rs.getPid()+"";
	    String tradeTime = rs.getTradeTime()+"";
	    String degree=rs.getDegree()+"";
	    String year = rs.getDate().substring(2, 4);
	    String year2= tx.toHexing(Integer.parseInt(year));
	    String month= rs.getDate().substring(4, rs.getDate().length()-2);
	    String month2=tx.toHexing(Integer.parseInt(month));
	    String day  = rs.getDate().substring(rs.getDate().length()-2);
	    String day2=tx.toHexing(Integer.parseInt(day));
	    String tradeTime2=tx.toHexing(Integer.parseInt(tradeTime));
	    String degree2 = tx.toHexing(Integer.parseInt(degree));
	    String result= "";
	    if(Integer.parseInt(day)<16&&rs.getTradeTime()<16){
	     result = pid+year2+month2+"0"+day2+"0"+tradeTime2+degree2;}
	    else if(Integer.parseInt(day)<16&&rs.getTradeTime()>=16)
	    {
		     result = pid+year2+month2+"0"+day2+tradeTime2+degree2;}
	    else if(Integer.parseInt(day)>=16&&rs.getTradeTime()<16)
	    { result = pid+year2+month2+day2+"0"+tradeTime2+degree2;}
	    else { result = pid+year2+month2+day2+tradeTime2+degree2;}
	  
		System.out.println(result);
	    byte[] ar = new byte[12];
	    
	    for (int i = 0;i<ar.length;i++)
	    {
	    	String temp = result.charAt(i)+"";
	    	
	    	ar[i] = (byte) Integer.parseInt(temp, 16);
	    }
	    
	    PbUtil.readPrintf(ar);
	    
	    //RFID����
//		mt.testinitreader();
//	    mt.testwrite(date);
		mt.Jreader.CloseReader();
	    
	    //��ݿ����
	    Rfid rfid = new Rfid();
	    rfid.setPid(rs.getPid());
	    rfid.setDate(rs.getDate());
	    rfid.setDegree(rs.getDegree());
	    rfid.setRfidnum(result);
	    rfid.setTradeTime(rs.getTradeTime());
	    rfidMapper.insertRfid(rfid);
	  
		
	}
	
	

}
